var testsFailed = 0;
var testsPassed = 0; 
var totalTests = 0; 

function assert(exp, message) {
  totalTests++; 
  if (!exp) {
      testsFailed++; 
      return "Test: " +totalTests+ "<br />"+ message + "<p style='color:red'>Fail</p>";
  }
  else { 
      testsPassed++; 
      return "Test: " +totalTests+ "<br />"+ message + "<p style='color:green'>Success</p>"; 
  }
}
function reservedWordTest(scanner, assumedType, inputString)
{
    scanner._resetScannerToDefault(); 
    scanner._setInputCode(inputString); 
    token = scanner._getToken();
    testMessage1 = 'Token Returned = {type : '+token.type+', lex : '+token.lex+'}';            
    testMessage2 = assert(token.type == assumedType, "<b>Assert:</b>   " +token.type+"="+assumedType+"<br />"); 
    testMessage3 = assert(token.lex == inputString, "<b>Assert:</b>   " +token.lex+"="+inputString+"<br />"); 
    messageString = '<hr width="100%"><div><h3>RW = '+inputString+'</h3><p>'+testMessage1+'</p><p>'+testMessage2+'</p><p>'+testMessage3+'</p></div><hr width=100%><br />'; 
    $('#actualResults').append(messageString); 
}

function symbolTest(scanner, assumedType, inputString)
{
    scanner._resetScannerToDefault(); 
    scanner._setInputCode(inputString); 
    token = scanner._getToken();
    testMessage1 = 'Token Returned = {type : '+token.type+', lex : '+token.lex+'}';            
    testMessage2 = assert(token.type == assumedType, "<b>Assert:</b>   " +token.type+"equiv to"+assumedType+"<br />"); 
    testMessage3 = assert(token.lex == inputString, "<b>Assert:</b>   " +token.lex+"equiv to"+inputString+"<br />"); 
    messageString = '<hr width="100%"><div><h3>RW equiv to '+inputString+'</h3><p>'+testMessage1+'</p><p>'+testMessage2+'</p><p>'+testMessage3+'</p></div><hr width=100%><br />'; 
    $('#actualResults').append(messageString); 
}

function tokenTest(scanner, assumedType, inputString) 
{
    scanner._resetScannerToDefault();  
    scanner._setInputCode(inputString);
    token = scanner._getToken();  
    testMessage1 = 'Token Returned = {type : '+token.type+', lex : '+token.lex+'}';             
    testMessage2 = assert(token.type == assumedType, "<b>Assert:</b>   " +token.type+"="+assumedType+"<br />"); 
    messageString = '<hr width="100%"><div><h3>String Tested = '+inputString+'</h3><p>'+testMessage1+'</p><p>'+testMessage2+'</p></div><hr width=100%><br />'; 
    $('#actualResults').append(messageString); 
}



function displayResults()
{
    $('#testStats').html(""); 
    statsMessage = "<div><p style=color:green;>Tests Succeeded : "+testsPassed+"</p><p style='color:red'>Tests Failed : "+testsFailed+"</p></div>";
    $('#testStats').append(statsMessage); 
}

 $(document).ready(function() {
      $( "button" ).button();
      $( "#reservedWordsTestsButton" ).click(function() { 	
           $('#actualResults').html(""); 
           $('#resultsHeader').html("Reserved Words Test"); 
           scanner = new Scanner(""); 
           
           assumedType = "RW_PROGRAM"; 
           inputString = "program";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_AND";
           inputString = "and";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_OR";
           inputString = "or";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_IF";
           inputString = "if";
           reservedWordTest(scanner, assumedType, inputString);  
           // Test End  
           
           // Test STart 
           assumedType = "RW_ELSIF";
           inputString = "elsif";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_ENDIF";
           inputString = "endif";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_WHILE";
           inputString = "while";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_ABS";
           inputString = "abs";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_NOT";
           inputString = "not";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_EXIT";
           inputString = "exit";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_ENDLOOP";
           inputString = "endloop";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_LOOP";
           inputString = "loop";
           reservedWordTest(scanner, assumedType, inputString);  
           
           // Test STart 
           assumedType = "RW_ENDPROGRAM";
           inputString = "endprogram";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_INTEGER";
           inputString = "integer";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_FLOAT";
           inputString = "float";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_TRUTHVALUE";
           inputString = "truthvalue";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_VAR_DECLARATIONS";
           inputString = "variable_declarations";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_END_OF_VAR_DECLARATIONS";
           inputString = "end_of_variable_declarations";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_CODE_START";
           inputString = "code_start";
           reservedWordTest(scanner, assumedType, inputString);  
           
           assumedType = "RW_CODE_END";
           inputString = "code_end";
           reservedWordTest(scanner, assumedType, inputString);  
           
           displayResults(); 
           testsPassed = 0; 
           testsFailed = 0; 
           totalTests = 0; 
            
      });
      $( "#stringTestsButton" ).click(function() { 	
           $('#actualResults').html(""); 
           $('#resultsHeader').html("String Test"); 
           scanner = new Scanner("");
           
           assumedType = "TOK_STR"; 
           inputString = "\"Sting with spaces test\"";
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_STR"; 
           inputString = "\"\"";
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_STR"; 
           inputString = "\"String with symbols #@$#*)(@@#@#%&*!*(\"";
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_RUNON_STR"; 
           inputString = "\"No Ending Quote Test";
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_RUNON_STR"; 
           inputString = "\"New Line Test\n";
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_RUNON_STR"; 
           inputString = "\"Ending quote with newline at end \n \"";
           tokenTest(scanner, assumedType, inputString); 
           displayResults(); 
           
           testsPassed = 0; 
           testsFailed = 0; 
           totalTests = 0; 
      }); 
      
      $( "#numberTestsButton" ).click(function() { 	
           $('#actualResults').html(""); 
           $('#resultsHeader').html("Number Scan Test"); 
           scanner = new Scanner("");
            
           assumedType = "TOK_INT" 
           inputString = "1"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_INT" 
           inputString = "0132432412"; 
           tokenTest(scanner, assumedType, inputString); 
          
           assumedType = "TOK_INT" 
           inputString = "5555*5555"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_INT" 
           inputString = "123."; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_INT" 
           inputString = "123abc"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_FIXED" 
           inputString = "123.343"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_FIXED" 
           inputString = "123.343abc"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_FIXED" 
           inputString = "1.1"; 
           tokenTest(scanner, assumedType, inputString); 
           displayResults(); 

            
           testsPassed = 0; 
           testsFailed = 0; 
           totalTests = 0; 
      }); 
      
      $( "#commentTestsButton" ).click(function() { 	
           $('#actualResults').html(""); 
           $('#resultsHeader').html("Comment Test"); 
           scanner = new Scanner("");
           
           assumedType = "TOK_COMMENT" 
           inputString = "--end of file comment test"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_COMMENT" 
           inputString = "--comment with symbols @$&*(%#*&(*&@#)(*!@%*(&#%)(*!#&)(*$&)(*$\"\"@"; 
           tokenTest(scanner, assumedType, inputString); 
           
           assumedType = "TOK_COMMENT" 
           inputString = "--comment with new line\n"; 
           tokenTest(scanner, assumedType, inputString); 
           displayResults(); 
            
           testsPassed = 0; 
           testsFailed = 0; 
           totalTests = 0; 
      }); 
      
      $( "#symbolTestsButton" ).click(function() { 	
           $('#actualResults').html(""); 
           $('#resultsHeader').html("Comment Test"); 
           scanner = new Scanner("");
 
           assumedType = "SYM_EQUALS" 
           inputString = "="; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_LEQ" 
           inputString = "<="; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_GEQ" 
           inputString = ">="; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_ASSIGN" 
           inputString = "<--"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_LTHAN" 
           inputString = "<"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_GTHAN" 
           inputString = ">"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_COLON" 
           inputString = ":"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_MULT" 
           inputString = "*"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_ADD" 
           inputString = "+"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_MINUS" 
           inputString = "-"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_DIVIDE" 
           inputString = "/"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_SEMI" 
           inputString = ";"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_LPAREN" 
           inputString = "("; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_RPAREN" 
           inputString = ")"; 
           symbolTest(scanner, assumedType, inputString); 
           
           assumedType = "SYM_COMMA" 
           inputString = ","; 
           symbolTest(scanner, assumedType, inputString); 

 
           displayResults(); 
           testsPassed = 0; 
           testsFailed = 0; 
           totalTests = 0; 
      }); 
});  
